package com.shujia.wyh;

import java.io.BufferedReader;
import java.io.FileReader;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import java.util.Properties;

/*
    通过读取配置文件获取数据库连接信息
    java xxx.jar xxx
 */
public class JDBCTest3 {
    public static void main(String[] args) throws Exception {
        //1、加载数据库的驱动
        Class.forName("com.mysql.jdbc.Driver");

        //读取配置文件获取url,用户名和密码
        Properties prop = new Properties();
        prop.load(new BufferedReader(new FileReader("E:\\projects\\IDEAProjects\\bigdata23-project\\bigdata23-project\\bigdata23-jdbc\\src\\main\\java\\com\\shujia\\wyh\\utils\\mysqlinfo.properties")));
        String url = prop.getProperty("url");
        String username = prop.getProperty("username");
        String password = prop.getProperty("password");

        //2、获取数据库的连接对象
        Connection conn = DriverManager.getConnection(url, username, password);

        //3、获取数据库操作对象
        Statement state = conn.createStatement();

        //4、编写sql语句，执行sql
        String sql = "insert into dept values(60,'兰智数加学院','安徽合肥')";
        int count = state.executeUpdate(sql);
        if(count==1){
            System.out.println("数据添加成功！！");
        }else {
            System.out.println("数据添加失败！！");
        }

        //6、释放资源
        state.close();
        conn.close();
    }
}
